package nl.jworks.epuborganizer.client.tabs;


import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.widget.core.client.TabItemConfig;
import com.sencha.gxt.widget.core.client.TabPanel;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.info.Info;
import nl.jworks.epuborganizer.client.GreetingService;
import nl.jworks.epuborganizer.client.GreetingServiceAsync;
import com.sencha.gxt.widget.core.client.event.SelectEvent;

public class BasicTabUiBinderExample implements IsWidget, EntryPoint {

    interface MyUiBinder extends UiBinder<Widget, BasicTabUiBinderExample> {
    }

    private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);

    @UiField(provided = true)
    String txt = "Test 3";

    @UiField(provided = true)
    TextButton push = new TextButton();

    public Widget asWidget() {
        return uiBinder.createAndBindUi(this);
    }

    public void onModuleLoad() {
        RootPanel.get().add(asWidget());
    }

    @UiHandler({"folder"})
    void onSelection(SelectionEvent<Widget> event) {
        TabPanel panel = (TabPanel) event.getSource();
        Widget w = event.getSelectedItem();
        TabItemConfig config = panel.getConfig(w);
        Info.display("Message", "'" + config.getText() + "' Selected");
    }

    @UiHandler({"push"})
    void onButtonClick(SelectEvent event) {
        final GreetingServiceAsync greetingService = GWT.create(GreetingService.class);
        greetingService.greetServer("hello", new AsyncCallback<String>() {
            @Override
            public void onFailure(Throwable throwable) {
                Info.display("Failure", throwable.toString());
            }

            @Override
            public void onSuccess(String s) {
                Info.display("Success", "Success!!!! " + s);
            }
        });

    }
}